<template>
    <div style="width: 100%; height: 100%">
        <div id="main" style="width: 100%; height: 100%"></div>

        <!-- 拖拽面板 -->
        <component
            :is="
                typeof item.name == 'string'
                    ? findCompByName(item.name)
                    : item.name
            "
            :key="index"
            :options="item.options"
            v-for="(item, index) in earthStore.pannellist">
        </component>

        <!-- 右下角快捷按钮 -->
        <Toolbox :toolboxOptions="props.toolboxOptions" v-if="showToolBox"/>

        <!-- 状态栏 -->
        <Earthstatusbar v-if="earthStore.stausbarshow && ifearth && showStatusBar" />

        <!-- 鹰眼图 -->
        <Eagleview v-if="earthStore.eagleviewshow && ifearth && showEagleView" />

        <!-- 分屏组件 -->

    </div>
</template>
<script setup>
import scenejson from './scene.json'
import { onMounted, nextTick, ref, onUnmounted } from 'vue'
import { useEarthStore } from '@/stores/earth'
import earth from './earth'
import { findCompByName } from '@/components/common/Earth/pannel/pannelList'

let coms = []

const props = defineProps({
    toolboxOptions: {
        type: Object,
        default: () => ({
            top: 100,
            right: 400,
        }),
    },
    doubleClickEvent: {
      type: Boolean,
      default: true
    },
    showToolBox: {
      type: Boolean,
      default: true
    },
    showEagleView: {
      type: Boolean,
      default: true
    },
    showStatusBar: {
      type: Boolean,
      default: true
    },
    mode2d: {
      type: Boolean,
      default: true
    },
})

const emit = defineEmits(['ready'])

const ifearth = ref(false)

const earthStore = useEarthStore()

let earthinstance = ''

// console.log('XbsjEarthUI======>', XbsjEarthUI)
onMounted(async () => {
    //加载cesium资源后初始化地球
  console.log(earthinstance, 'earthinstance')
    earthinstance = new earth(
        'main',
        {
            contextOptions: {
              webgl: {
                alpha: true,
                depth: false,
                stencil: true,
                antialias: true,
                premultipliedAlpha: true,
                preserveDrawingBuffer: true,
                failIfMajorPerformanceCaveat: true,
              },
            },
            animation: true,
            timeline: true,
        },
        scenejson,
    )

    earthinstance.getViewerReady().then((res) => {
        ifearth.value = true
        if (!props.doubleClickEvent) {
          viewer.screenSpaceEventHandler.removeInputAction(
            Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
          );
        }

        if (props.mode2d) {
          viewer.scene.mode = Cesium.SceneMode.SCENE2D
          // viewer.scene.mode = Cesium.SceneMode.COLUMBUS_VIEW
          // viewer.scene.morphTo2D(1);
        }
        emit('ready')
    })
})

onUnmounted(() => {
    earthinstance.destroy()
    earthStore.$reset()
    window.viewer = null
})
</script>
